package com.artPreter.controller.mobile;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.artPreter.utils.PageUtils.collectPageList3;
import static com.artPreter.utils.PageUtils.getPageList;

@Controller
@RequestMapping("/m/index")
public class MIndexContorller {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IIndexItemRelativeService indexItemRelativeService;
    @Autowired
    private ISystemConfigService systemConfigService;
    @Autowired
    private IEventExhibitionService eventExhibitionService;
    @Autowired
    private IEventExhibitionBannerService eventExhibitionBannerService;
    @Autowired
    private IDepartTypeService departTypeService;
    @Autowired
    private IArtworkOtherPicService artworkOtherPicService;

    @RequestMapping("")
    public String index(Model model) {

        // 首页轮播
        List<Banner> banners = bannerService.getList(99);
        model.addAttribute("banners",banners);

        // 活动展览
        List<EventExhibition> eventExhibitions = indexItemRelativeService.getIndexContentEventExhibitions("1b3a29a9-1c40-4dd5-9905-ba78fa6bfb24",1);
        model.addAttribute("eventExhibitions",eventExhibitions);

        // 艺术评论
        IPage<Article> articleIPage1 = articleService.getPageRelativeFull(1,99,"9aca26f3-8e3e-40b1-8ffb-f2149cb33578");
        model.addAttribute("articleList1",articleIPage1.getRecords());
        // 市场资讯
        IPage<Article> articleIPage2 = articleService.getPageRelativeFull(1,99,"dfdfcc44-689a-4b6e-9d86-881c4bed4743");
        model.addAttribute("articleList2",articleIPage2.getRecords());

        // 首页作品在线
        List<Artwork> onLineArtworks = indexItemRelativeService.getIndexContentArtworks("830388e7-538a-45f5-9201-cf2c67117674",8);
        model.addAttribute("onLineArtworks",onLineArtworks);

        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);

        // 首页显示机构 4条
        List<Depart> indexDeparts = departService.getIndexDeparts(16);
        model.addAttribute("departs",indexDeparts);


        return "mobile/index/index";
    }

    @RequestMapping("/event/list")
    public String indexEvent(String name,Integer type,Integer currentPage, Model model) {

        if(currentPage == null){
            currentPage = 1;
        }

        // 首页轮播
        List<EventExhibitionBanner> banners = eventExhibitionBannerService.getList(99);
        model.addAttribute("banners",banners);

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getContent().equals("/event/list")){
                menuList.get(i).setIsActice(1);
            }
        }
        model.addAttribute("menus",menuList);
        // 活动展览
        EventExhibition eventExhibition = new EventExhibition();


        IPage<EventExhibition> eventExhibitionIPage = eventExhibitionService.getPage(eventExhibition,currentPage);

        // 数据
        List<EventExhibition> records = eventExhibitionIPage.getRecords();
        for (int l = 0; l < records.size(); l++) {
            if(records.get(l).getEndTime().isBefore(LocalDateTime.now())){
                records.get(l).setIsOver(1);
            }
            List<Artist> list = new ArrayList<>();
            if(records.get(l).getArtistIds()!=null){
                String[] strings1 = records.get(l).getArtistIds().split("，");
                String[] strings2 = records.get(l).getArtistNames().split("，");
                for (int z = 0; z < strings1.length; z++) {
                    if(!strings1[z].equals("")){
                        Artist artist = new Artist();
                        artist.setId(Integer.valueOf(strings1[z]));
                        artist.setName(strings2[z]);
                        list.add(artist);
                    }

                }
            }
            records.get(l).setList(list);
        }


        model.addAttribute("eventExhibitions",eventExhibitionIPage.getRecords());
        // 当前页
        model.addAttribute("currentPage",eventExhibitionIPage.getCurrent());
        // 总页数
        model.addAttribute("totalPages",eventExhibitionIPage.getPages());
        // 分页页码
        model.addAttribute("pages",getPageList((int) eventExhibitionIPage.getPages(),currentPage,5));


        return "mobile/index/event_exhibition_list";
    }


    @RequestMapping("/depart/list")
    public String departList(Integer aregion, String name,Integer departType, Model model) {
        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);


        Depart depart = new Depart();
        // 区域搜索
        if (aregion == null || aregion.equals(0)) {
            model.addAttribute("aregion",0);
            aregion = null;
        }else{
            depart.setRegion(aregion);
            model.addAttribute("aregion",aregion);
        }


        // 名称搜索

        if (name == null||name.equals("")||name.equals("null")) {
            name = "";
        }else{
            depart.setName(name);
        }
        model.addAttribute("name",name);
        // 机构类别
        List<DepartType> departTypes = departTypeService.getDepartTypeList(10);
        for (int i = 0; i < departTypes.size(); i++) {
            if(departTypes.get(i).getId()!=null){
                if(departTypes.get(i).getId().equals(departType)){
                    depart.setDepartType(departTypes.get(i).getTypeId());
                }

            }
        }
        if(departType==null){
            model.addAttribute("departTypeId",0);
        }else{
            model.addAttribute("departTypeId",departType);
        }


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        menuList.get(1).setIsActice(1);
        model.addAttribute("menus",menuList);

        // 区域
        List<Region> provinceList = new ArrayList<>();
        List<Region> cityList = new ArrayList<>();
        List<Region> regions = departService.getDepartRegionList(10);

        Integer pid = -1;


        for (int i = 0; i < regions.size(); i++) {
            Region region = regions.get(i);
            if(region.getId().equals(aregion)){
                if(region.getType().equals(2)){
                    pid = region.getPid();
                }
            }
        }
        model.addAttribute("pid",pid);

        for (int i = 0; i < regions.size(); i++) {
            Region region = regions.get(i);
            if(region.getType().equals(1)){
                provinceList.add(region);
            }
            if(aregion!=null && (aregion.equals(region.getPid())||pid.equals(region.getPid()) )&& region.getType().equals(2)){
                cityList.add(region);
            }
        }
        model.addAttribute("regions",regions);
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("cityList",cityList);



        model.addAttribute("departTypes",departTypes);

        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);
        List<Depart> indexDeparts = departService.getDepartList(depart);
        if(pid!=-1||aregion==null){

            model.addAttribute("indexDeparts",indexDeparts);
        }else{

            List<Depart> departList = departService.getDepartListWithPid(depart);
            model.addAttribute("indexDeparts",departList);
        }


        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);




        return "mobile/index/depart_list";
    }

    @RequestMapping("/artist/list")
    public String artistList( String name,String fy,String atype,Integer currentPage, Model model) throws UnsupportedEncodingException {
        if(name == null||name.equals("")||name.equals("null")){
            name = "";
        }
        if(fy == null||fy.equals("")||fy.equals("null")){
            fy = "";
        }
        if(atype == null||atype.equals("")||atype.equals("null")){
            atype = "";
        }

        if(currentPage == null){
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        List<ArtType> artTypes = new ArrayList<>();
        menuList.get(2).setIsActice(1);
        model.addAttribute("menus",menuList);
        Artist artist = new Artist();
        artist.setName(name);
        artist.setFirstPy(fy);
        artist.setArtistType(atype);
        Integer size = 36;

        IPage<Artist> artistIPage = artistService.getPage(currentPage,size,artist);
        model.addAttribute("currentPage",artistIPage.getCurrent());
        artistIPage.getPages();
        System.out.println(3);
        // 分页页码
        Integer jump = 3;
        if(currentPage==1||currentPage==2){
            jump = 2;
        }else if(currentPage>2){
            jump = 1;
        }
        model.addAttribute("pages",getPageList((int) artistIPage.getPages(),currentPage,jump));
        // 总页数
        model.addAttribute("totalPages",artistIPage.getPages());
        // 分页数据
        model.addAttribute("artists",artistIPage.getRecords());
        model.addAttribute("keyword",name);
        model.addAttribute("atype",atype);
        model.addAttribute("fy",fy);
        // 首页显示艺术类型
        artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);
        return "mobile/index/artist_list";


    }

    @RequestMapping("/page")
    public String indexPage(Integer cid, Model model) {

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        String contentId = null;
        String content = "";
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getId().equals(cid)){
                menuList.get(i).setIsActice(1);
                contentId = menuList.get(i).getContentId();
                content = menuList.get(i).getPageDetail();
                System.out.println(content);
            }
        }

        model.addAttribute("menus",menuList);
        model.addAttribute("cid",cid);




        model.addAttribute("pageDetail",content);
        if(contentId==null){
            return "redirect:/";
        }
        return "mobile/index/page";
    }


    @RequestMapping("/article/list")
    public String indexArticle(Integer cid, Model model) {

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        String contentId = null;

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getId().equals(cid)){
                menuList.get(i).setIsActice(1);
                contentId = menuList.get(i).getContentId();
            }
        }
        model.addAttribute("menus",menuList);
        model.addAttribute("cid",cid);


        Integer currentPage = 1;
        System.out.println(contentId+"id");
        IPage<Article> articleIPage = articleService.getPageRelativeFull(currentPage,99,contentId);
        System.out.println(articleIPage.getRecords().size()+"-------------");
        model.addAttribute("articles",articleIPage.getRecords());
        if(contentId==null){
            return "redirect:/m/index";
        }
        // return "pc/company/simple_article_list";
        return "mobile/index/article_list";
    }

    @RequestMapping("/artwork/online")
    public String index(String name,Integer type,Integer currentPage, Model model) {
        Artwork artwork = new Artwork();


        if(currentPage == null){
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        List<ArtType> artTypes = new ArrayList<>();
        menuList.get(4).setIsActice(1);
        // 首页显示艺术类型
        artTypes = artTypeService.getList();


        if(type!=null){
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getId().equals(type)){
                    artwork.setArtwoktType(artTypes.get(i).getTypeId());
                }
            }
        }
        if(name == null||name.equals("")||name.equals("null")){
            name = "";
        }else{
            artwork.setName(name);
        }
        artwork.setType(-1);
        Integer artWorkSize = 16;
        IPage<Artwork> artworkIPage =  artWorkService.getPage(currentPage,artWorkSize,artwork);
        model.addAttribute("artTypes",artTypes);
        // 当前页
        model.addAttribute("currentPage",artworkIPage.getCurrent());
        // 总页数
        model.addAttribute("totalPages",artworkIPage.getPages());
        // 分页页码
        List<Number> pageList = getPageList((int) artworkIPage.getPages(), currentPage, 3);
        pageList = collectPageList3(pageList, (int) artworkIPage.getPages(),currentPage);
        model.addAttribute("pages",pageList);
        // 分页数据
        model.addAttribute("artworks",artworkIPage.getRecords());
        model.addAttribute("name",name);
        model.addAttribute("type",type);

        return "mobile/index/online_list";
        //return "pc/company/artwork_online_list";

    }

    /**
     *
     * @param id 艺术品id
     * @param
     * @param model
     * @return
     */
    @GetMapping("/online/artwork/detail")
    public String onlineArtworkDetail(Integer id, Model model) {
        System.out.println(id+"作品在线id");
        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        for (int i = 0; i < menuList.size(); i++) {
            System.out.println(menuList.get(i).getContent());
            if(menuList.get(i).getContent().equals("/artwork/online")){
                menuList.get(i).setIsActice(1);
                model.addAttribute("backName",menuList.get(i).getName());
                model.addAttribute("backContent",menuList.get(i).getContent());
            }
        }


        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);

        Artwork artwork = artWorkService.getArtWorkById(id);



        if(artwork!=null&&artwork.getId()!=null){
            if(artwork.getArtwoktType()!=null) {
                for (int i = 0; i < artTypes.size(); i++) {
                    if(artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())){
                        artwork.setArtTypeName(artTypes.get(i).getName());
                    }
                }
            }
            List<ArtworkOtherPic> artworkOtherPics = artworkOtherPicService.getArtworkAllPicList(artwork);
            model.addAttribute("artworkOtherPics",artworkOtherPics);
            System.out.println(artworkOtherPics.size()+"------------");
            model.addAttribute("artwork",artwork);
            //return "pc/company/artwork_online_detail";
            return "mobile/index/online_detail";
        } else {
            return "redirect:/";
        }
    }


    @GetMapping("/article")
    public String toArticle(Integer id, Model model) {
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);

        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);


        List<Article> articles = articleService.getArticleListById(id);
        System.out.println(id+"id");
        if(articles.size()>0){
            Article article = articles.get(0);

            if(article.getLink()!=null&&!article.getLink().equals("")){
                // 文章链接
                String link = article.getLink();
                return "redirect:"+link;
            }



            model.addAttribute("article",article);
            return "mobile/index/article_detail";
        } else {
            return "redirect:/m/index";
        }
    }

    @GetMapping("/search")
    public String search(Integer type,String name, Model model) {
        System.out.println(name+"搜索关键字");
        System.out.println(type+"搜索类型");

        switch (type){
            default:
                return "redirect:/m/index";
        }
    }





}
